package com.cheil.publish.web;

import com.alibaba.fastjson2.JSON;
import com.cheil.publish.service.ai.DashscopeImageGenerationMetadata;
import com.cheil.publish.service.ai.DashscopeService;
import com.cheil.publish.service.data.ChatsDto;
import com.cheil.publish.service.publish.AutoPublishService;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.ai.image.ImageGeneration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;

/**
 * @author ssk
 * @date 2025/9/5
 **/
@Tag(name = "chat")
@Controller
@AllArgsConstructor
@Log4j2
public class ChatController {

    private final DashscopeService dashscopeService;
    private final AutoPublishService autoPublishService;

    @GetMapping("/chat")
    public String hello() {
        return "chat";
    }

    @GetMapping("/login")
    @ResponseBody
    @ApiResponse(responseCode = "200", description = "返回Base64编码的二维码图片",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ImageResponse.class)))
    public ResponseEntity<ImageResponse> login() throws InterruptedException {
        String imageData = autoPublishService.login();
//        String base64Image = Base64.getEncoder().encodeToString(imageData);
//        log.info("二维码图片数据: {}", base64Image);

        return ResponseEntity.ok(new ImageResponse(imageData, "image/png"));
    }

    // 内部类用于包装图片响应
    public static class ImageResponse {

        private String imageData;
        private String mimeType;

        public ImageResponse() {
        }

        public ImageResponse(String imageData, String mimeType) {
            this.imageData = imageData;
            this.mimeType = mimeType;
        }

        public String getImageData() {
            return imageData;
        }

        public void setImageData(String imageData) {
            this.imageData = imageData;
        }

        public String getMimeType() {
            return mimeType;
        }

        public void setMimeType(String mimeType) {
            this.mimeType = mimeType;
        }
    }

    @GetMapping("/login-base64")
    @ResponseBody
    public ResponseEntity<String> loginBase64() throws InterruptedException {
//        byte[] imageData = autoPublish.login();
String imageData = autoPublishService.login();
//        String base64Image = Base64.getEncoder().encodeToString(imageData);
        return ResponseEntity.ok("data:image/png;base64," + imageData);
    }
    @GetMapping("/publish")
    public Flux<ChatsDto> publish(@RequestParam String message) {
        ChatsDto chat = dashscopeService.chat(message);
        org.springframework.ai.image.ImageResponse response = dashscopeService.generateImage(chat.content());
        log.info("chat {}", JSON.toJSONString(chat));
        log.info("image {}", JSON.toJSONString(response));
        ImageGeneration result = response.getResult();
        DashscopeImageGenerationMetadata metadata = (DashscopeImageGenerationMetadata) result.getMetadata();
        ChatsDto chatsDto = new ChatsDto(
                chat.title(),
                chat.content(),
                result.getOutput().getUrl(),
                metadata.getRevisedPrompt(),
                chat.tags()
        );

        autoPublishService.publish(chatsDto);
        return Flux.just(chatsDto);
    }
}